package edu.hit.pact.pgse.crawler.bbs;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.commons.logging.Log;

import edu.hit.pact.pgse.crawler.download.WebPageFetcher;
import edu.hit.pact.pgse.crawler.store.FileSystemWebPageStore;
import edu.hit.pact.pgse.crawler.store.WebPageStore;

public class BBSCrawlerThreadManager extends AbstractBBSFetchorThread {

	private AbstractBBSFetchorThread indexThread = null;
	private List<AbstractBBSFetchorThread> articleThreads;
	private static final int ARTICLETHREADNUM = 4;
	private ConcurrentLinkedQueue<URL> articleUrlQueue;
	private URL indexHomepage;
	private int threadno = 0;

	public BBSCrawlerThreadManager(String threadName, Log logger) {
		super(threadName, logger);
		articleUrlQueue = new ConcurrentLinkedQueue<URL>();
		articleThreads = new ArrayList<AbstractBBSFetchorThread>();
	}

	public URL getIndexHomepage() {
		return indexHomepage;
	}

	public void setIndexHomepage(URL indexHomepage) {
		this.indexHomepage = indexHomepage;
	}

	@Override
	public void run() {
		logger.info("BBSCrawler Manager begin to work.");
		mainRun();
		logger.info("BBSCrawler Manager has finished");
	}

	public void mainRun() {

		WebPageFetcher indexFetcher = buildWebPageFetcher();
		WebPageStore indexStore = new FileSystemWebPageStore(true);
		indexThread = new BBSCrawlerIndexFetchProcessor(logger, indexFetcher,
				indexStore, articleUrlQueue, "indexThread");
		indexThread.setHomepage(indexHomepage);
		indexThread.start();

		for (int i = 0; i < ARTICLETHREADNUM; i++) {
			WebPageFetcher articleFetcher = buildWebPageFetcher();
			WebPageStore articleStore = new FileSystemWebPageStore(true);
			articleThreads.add(new BBSArticleFetchProcessor(logger,
					articleFetcher, articleStore, articleUrlQueue,
					"articleThread " + threadno++,"free"));
		}

		for (AbstractBBSFetchorThread thread : articleThreads) {
			thread.start();
		}
		while (true) {
			logger.info("bbs crawler manager to check..");
			int diff = ARTICLETHREADNUM - aliveThreadCnt.intValue();
			if (diff > 0) {
				WebPageFetcher articleFetcher = buildWebPageFetcher();
				WebPageStore articleStore = new FileSystemWebPageStore(true);
				new BBSArticleFetchProcessor(logger, articleFetcher,
						articleStore, articleUrlQueue, "articleThread "
								+ threadno++,"free").start();
				logger.info("new bbs crawler is build..");
			}
			try {
				sleep(100000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

}
